package com.zxm.web.controller.txcloud.ocr;

import com.zxm.common.core.domain.AjaxResult;
import com.zxm.common.core.domain.TxyFactory;
import com.zxm.common.core.domain.entity.TxyCardOcr;
import com.zxm.common.utils.StringUtils;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author silence.Z
 * @Date 2022/6/9 16:17
 * @PackageName:com.zxm.web.controller.txcloud.ocr
 * @ClassName: CardController
 * @Description: 卡证文字识别
 * @Version 1.0
 */
@RestController
@RequestMapping("txcloud/ocr/card")
public class CardController {
	private static final Logger log = LoggerFactory.getLogger(CardController.class);


	@PreAuthorize("@ss.hasPermi('txcloud:ocr:card:list')")
	@PostMapping("idCardOCR")
	/**
	 * 身份证识别
	 */
	public AjaxResult idCardOCR(@Validated @RequestBody TxyCardOcr cardOcr) throws TencentCloudSDKException {
		OcrClient client = TxyFactory.createOcr();
		IDCardOCRRequest req = new IDCardOCRRequest();
		if (StringUtils.isNotEmpty(cardOcr.getImageBase64())){
			req.setImageBase64(cardOcr.getImageBase64());
		}
		if (StringUtils.isNotEmpty(cardOcr.getImageUrl())){
			req.setImageUrl(cardOcr.getImageUrl());
		}
		if (StringUtils.isNotEmpty(cardOcr.getCardSide())){
			req.setCardSide(cardOcr.getCardSide());
		}
		IDCardOCRResponse resp = client.IDCardOCR(req);
		// 输出json格式的字符串回包
		log.info(IDCardOCRResponse.toJsonString(resp));
		return AjaxResult.success(resp);
	}

	@PreAuthorize("@ss.hasPermi('txcloud:ocr:card:list')")
	@PostMapping("bankCardOCR")
	/**
	 * 银行卡识别
	 */
	public AjaxResult bankCardOCR(@Validated @RequestBody TxyCardOcr cardOcr) throws TencentCloudSDKException {
		OcrClient client = TxyFactory.createOcr();
		BankCardOCRRequest req = new BankCardOCRRequest();
		if (StringUtils.isNotEmpty(cardOcr.getImageBase64())){
			req.setImageBase64(cardOcr.getImageBase64());
		}
		if (StringUtils.isNotEmpty(cardOcr.getImageUrl())){
			req.setImageUrl(cardOcr.getImageUrl());
		}
		BankCardOCRResponse resp = client.BankCardOCR(req);
		// 输出json格式的字符串回包
		log.info(BankCardOCRResponse.toJsonString(resp));
		return AjaxResult.success(resp);

	}
}
